const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dishi: 0,
    dishis: ['请选择报考地区', '省直', '济南', '青岛', '淄博', '枣庄', '东营', '烟台', '潍坊', '济宁', '泰安', '威海', '日照', '滨州', '德州', '聊城', '临沂', '菏泽'],
    zkz: ''
  },
  dishisChange: function (e) {
    console.log('dishis发送选择改变，携带值为', e.detail.value)
    this.setData({
      dishi: e.detail.value
    })
  },
  zkzInput: function (e) {
    this.setData({
      zkz: e.detail.value
    })
    console.log(this.data.zkz)
  },

  onLoad: function (options) {
    var _ = this;
    console.log(options);
    _.setData({
      area: options.area,
      geneal: options.agent
    })
    if (options.page) {
      _.setData({
        page: options.page
      })
    }
  },
  getPhoneNumber(e) {
    var _ = this;
    var diqu = _.data.dishis[_.data.dishi];
    var zkz = _.data.zkz;
    if (zkz == '') {
      wx.showToast({
        title: '请填写准考证号！',
        icon:'none'
      })
    } else {
      wx.cloud.callFunction({
        name: 'getMobile',
        data: {
          weRunData: wx.cloud.CloudID(e.detail.cloudID),
        }
      }).then(res => {
        _.setData({
          mobile: res.result,
          phone: res.result,
        })
        wx.setStorageSync('phone', res.result)
        console.log(res.result)

        wx.request({
          url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
          method: 'POST',
          data: {
            activeId: 39,
            mobile: res.result,
            areaCode: _.data.area,
            agent: _.data.geneal,
            pageId: _.data.page
          },
          success: res => {
            console.log(res);
          }
        })

        wx.request({ //发送请求
          url: 'https://zg99.offcn.com/index/biaodan/register?actid=45127&callback=?', //接收url
          data: {
            phone: res.result,
            fenxiao: _.data.area,
            geneal: _.data.geneal,
          }, //接收参数
          success(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
            }
            console.log(theJson)
          }
        })

        var fenshuSt = {
          diqu,
          zkz
        }
        wx.setStorageSync('fenshuSt', fenshuSt)
        console.log(diqu)

        wx.request({ //发送请求
          url: "https://zg99.offcn.com/index/shaifen/register/?actid=45129&callback=?",
          data: {
            zkz: zkz,
            phone: _.data.mobile
          }, //接收参数
          success(res) { //返回
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
            };
            console.log(theJson)
            wx.request({ //发送请求
              url: "https://zg99.offcn.com/index/shaifen/getfzinfo/?actid=45129&callback=?",
              data: {
                diqu: diqu
              }, //接收参数
              success(res) { //返回
                var jsonStr = res.data;
                jsonStr = jsonStr.replace("(", "");
                jsonStr = jsonStr.replace(")", "");
                if (typeof jsonStr != 'object') {
                  jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                  var theJson = JSON.parse(jsonStr);
                };
                console.log(theJson)
                if (theJson.status == 1) {
                  if (zkz.length == 13) {
                    wx.request({ //发送请求
                      url: "https://zg99.offcn.com/index/shaifen/getfzinfo/?actid=45129&callback=?",
                      data: {
                        zkz: zkz
                      }, //接收参数
                      success(res) { //返回
                        var jsonStr = res.data;
                        jsonStr = jsonStr.replace("(", "");
                        jsonStr = jsonStr.replace(")", "");
                        if (typeof jsonStr != 'object') {
                          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                          var theJson = JSON.parse(jsonStr);
                        };
                        console.log(theJson)

                        if (theJson.status == 1) {
                          if (theJson.lists.length == 1) {
                            console.log('real success!')
                            wx.navigateTo({
                              url: '../sues/sues',
                            })
                          } else {
                            console.log('error success!')
                            wx.navigateTo({
                              url: '../fals/fals',
                            })
                          }
                        } else {
                          console.log('error')
                          wx.navigateTo({
                            url: '../fals/fals',
                          })
                        }
                      }
                    })


                  } else {
                    wx.showToast({
                      title: '请输入正确的准考证号！',
                      icon: 'none',
                      duration: 2000
                    })
                  }
                } else {
                  wx.navigateTo({
                    url: '../fenshu/fenshu',
                  })
                }
              }
            })
          }
        })


      }).catch(err => {
        console.error(err);
      });


    }

  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '2022山东省考查排名知分差',
      path: 'pages/shaifen/index/index?area=' + this.data.area + '&agent=' + this.data.geneal + '&page=' + this.data.page + '&by_qr=2'
    }
  },
  /**分享到朋友圈*/
  onShareTimeline: function () {
    return {
      title: '2022山东省考查排名知分差',
      path: 'pages/shaifen/index/index?area=' + this.data.area + '&agent=' + this.data.geneal + '&page=' + this.data.page + '&by_qr=2'
    }
  }
})